package springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springboot.Tools.ClientDataApi;
import springboot.Tools.Date.StartAndEndTime;
import springboot.Tools.JSONToSETime.CustomTime;
import springboot.Tools.Timer.TimerSocketMessage;
import springboot.Tools.time;
import springboot.doMain.ClientGroupByDayData;
import springboot.doMain.OverTimeData;
import springboot.service.impl.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class vueTest {
	ClientDataApi clientDataApi = new ClientDataApi();
	
	final
	WXGroupServiceImpl wxGroupService;
	final
	WXUserServiceImpl wxUserService;
	
	final
	OverTimeDataServiceImpl overTimeDataService;
	
	final
	MsgServiceImpl msgService;
	
	final
	ClientGroupByDayDataServiceImpl clientGroupByDayDataService;
	
	final
	WXGroupChatAdminServiceImpl wxGroupChatAdminService;
	
	public vueTest(ClientGroupByDayDataServiceImpl clientGroupByDayDataService,
	               OverTimeDataServiceImpl overTimeDataService, MsgServiceImpl msgService,
	               WXGroupChatAdminServiceImpl wxGroupChatAdminService,
	               WXGroupServiceImpl wxGroupService, WXUserServiceImpl wxUserService) {
		this.clientGroupByDayDataService = clientGroupByDayDataService;
		this.overTimeDataService = overTimeDataService;
		this.msgService = msgService;
		this.wxGroupChatAdminService = wxGroupChatAdminService;
		this.wxGroupService = wxGroupService;
		this.wxUserService = wxUserService;
	}
	
	@ResponseBody
	@GetMapping("getOverTimeData")
	public List<OverTimeData> getOverTimeData() {
		return OverTimeData.fillList(overTimeDataService.selLastData());
	}
	
	@RequestMapping("/clientData")
	public String clientData() {
		return "CD";
	}
	
	
	@ResponseBody
	@GetMapping("getClientDataByDay")
	public Map<Object, Object> getClientDataByDay(String dataOption) {
		if (dataOption == null) {
			return null;
		}
		Map<Object, Object> map = new HashMap<>();
		try {
			CustomTime customTime = clientDataApi.getCustomTime(dataOption);
			List<ClientGroupByDayData> clientGroupByDayData = clientGroupByDayDataService.selClientGroupByDayData(
							customTime.getStartTime().getTime() /
											1000, customTime.getEndTime().getTime() / 1000);
			map.put("startTime", time.dateFormat(customTime.getStartTime(), 1));
			map.put("endTime", time.dateFormat(customTime.getEndTime(), 1));
			map.put("items", clientGroupByDayData);
			map.put("errCode", 1);
		} catch (Exception e) {
			map.put("errCode", -2);
			map.put("errMsg", e);
		}
		return map;
	}
	
	
	@GetMapping("/getGroupLiveSevenDays")
	@ResponseBody
	public Map<String, Object> getGroupLiveSevenDays(Integer diff) {
		Map<String, Object> map = new HashMap<>();
		List<String> daysString = StartAndEndTime.getDaysString(7, diff - 1);
		Map<Object, Object> sellerDataByAnother = getSellerDataByAnother(diff);
		/*群总数*/
		map.put("allGroupCnt", wxGroupService.selAllGroupCnt());
		map.put("sellerData", sellerDataByAnother);
		map.put("date", daysString);
		map.put("errCode", 0);
		map.put("errMsg", "");
		new Thread(this::insertSellerData).start();
		return map;
	}
	
	@GetMapping("getWeekSellerData")
	@ResponseBody
	public List<Map<String, Object>> getWeekSellerData(int diff) {
		return wxGroupChatAdminService.selSellerData(diff);
	}
	
	
	/*查找销售数据*
	public Map<Object, Object> getSellerData(Integer diff) {
		Map<Object, Object> map = new HashMap<>();
		Day day;
		day = StartAndEndTime.getDay(diff - 1);
		List<Map<String, Object>> maps = wxGroupChatAdminService.selPeriodSellerData(day);
		Map<Object, List<Map<String, Object>>> collect = maps.stream()
				.collect(Collectors
						.groupingBy(stringObjectMap -> stringObjectMap.get("day_time")));
		collect.forEach((o, maps1) -> {
			Map<Object, List<Map<String, Object>>> remark = maps1.stream()
					.collect(Collectors.groupingBy(stringObjectMap -> stringObjectMap.get("URemark")));
			map.put(o.toString().replace("T00:00", ""), remark);
		});
		return map;
	}*/
	
	/*查找销售数据*/
	public Map<Object, Object> getSellerDataByAnother(Integer diff) {
		List<Map<String, String>> maps = wxGroupChatAdminService.selPeriodSellerDataByAnother(diff);
		if (maps == null) {
			return null;
		}
		Map<Object, Object> objectObjectHashMap = new HashMap<>();
		Map<Object, List<Map<String, String>>> day_time =
						maps.stream().collect(Collectors.groupingBy(map -> map.get("day_time")));
		day_time.forEach((s, maps1) -> {
			Map<String, List<Map<String, String>>> remark =
							maps1.stream().collect(Collectors.groupingBy(map -> map.get("remarked")));
			objectObjectHashMap.put(s.toString().replace("T00:00", ""), remark);
		});
		return objectObjectHashMap;
	}
	
	
	@ResponseBody
	@GetMapping("getBigGroupTotal")
	public String getBigGroupTotal(Integer start) {
		Map<String, String> map = clientGroupByDayDataService.selBigGroupTotal(start);
		return String.valueOf(map.get("cnt"));
	}
	
	private void insertSellerData() {
		TimerSocketMessage.insertSellerData(wxUserService, wxGroupChatAdminService);
	}
}
